{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "您还可以通过添加@chain装饰器将任意函数转换为链式函数。这在功能上等同于在RunnableLambda中进行包装。这将通过正确跟踪您的链式函数来改善可观察性。在此函数内部对可运行对象的任何调用都将被跟踪为嵌套子项。它还允许您像使用其他可运行对象一样使用它，将其组合在链式函数中等等。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f188477f3ac27e70"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "from langchain_core.runnables import chain\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_community.llms.tongyi import Tongyi\n",
    "from dotenv import load_dotenv\n",
    "import os\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "prompt1 = ChatPromptTemplate.from_template(\"Tell me a joke about {topic}\")\n",
    "prompt2 = ChatPromptTemplate.from_template(\"What is the subject of this joke: {joke}\")\n",
    "\n",
    "@chain\n",
    "def custom_chain(text):\n",
    "    prompt_val1 = prompt1.invoke({\"topic\": text})\n",
    "    output1 = ChatOpenAI(\n",
    "        # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "        openai_api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "        openai_api_base=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "        model_name=\"qwen-max\"\n",
    "    ).invoke(prompt_val1)\n",
    "    parsed_output1 = StrOutputParser().invoke(output1)\n",
    "    chain2 = prompt2 | ChatOpenAI(\n",
    "        # 若没有配置环境变量，请用百炼API Key将下行替换为：api_key=\"sk-xxx\",\n",
    "        openai_api_key=os.getenv(\"DASHSCOPE_API_KEY\"),\n",
    "        openai_api_base=\"https://dashscope.aliyuncs.com/compatible-mode/v1\",\n",
    "        model_name=\"qwen-max\"\n",
    "    ) | StrOutputParser()\n",
    "    return chain2.invoke({\"joke\": parsed_output1})"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-28T06:59:30.737425Z",
     "start_time": "2024-10-28T06:59:28.517228Z"
    }
   },
   "id": "17a615a66a35243b",
   "execution_count": 2
  },
  {
   "cell_type": "markdown",
   "source": [
    "`custom_chain`现在是一个可运行对象，这意味着您需要使用invoke来调用它。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4364507528d3ac50"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "'The subject of this joke is bears, and it plays on the puns related to their behavior and physical characteristics, specifically using \"paws\" as a play on words for \"go.\" The humor comes from imagining bears trying to deal with human situations like waiting in line at a fast food restaurant, while also incorporating a bear-specific twist.'"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "custom_chain.invoke(\"bears\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-10-28T06:59:46.837696Z",
     "start_time": "2024-10-28T06:59:37.497848Z"
    }
   },
   "id": "f819bfc33bfa4199",
   "execution_count": 3
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "a9d3bb259b3a3e1d"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
